// pages/clientInfo/clientInfo.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    khid:'',//客户ID
    active:0,
    show:false,
    actions:[{ name: '转给他人'},{ name: '转到公客池'}],
    tags:'',
  },
  onChange(event) {
     this.setData({
       active:event.detail.name
     })
     this.requestData(this.data.khid);
  },
  showAction(){
    this.setData({
      show:true
    })
  },
  cancelAction(){
    this.setData({
      show:false
    })
  },
  contactDetail(e){
    wx.navigateTo({
      url: '../contactsInfo/contactsInfo?id='+e.currentTarget.dataset.id+'&khid='+this.data.khid,
    })
  },
  /**选项 */
  selectMenu(event){
    let name = event.detail.name;
    if(name=='转给他人'){
      wx.navigateTo({
        url: '../chooseStaff/chooseStaff?id='+this.data.khid+'&type=client',
      })
    }else if(name=='转到公客池'){
      api.curlPost('?cla=clientAppClass&fun=poolEdit',{khid:this.data.khid}).then((res)=>{
        wx.navigateBack({
          delta:1
        })
      })
    }
  },
  directionTo(e){
    let type = e.currentTarget.dataset.type;
    if(type=='编辑'){
      wx.navigateTo({
        url: '../clientEdit/clientEdit?id='+this.data.khid,
      })
    }else if(type=='联系人'){
      wx.navigateTo({
        url: '../contactsEdit/contactsEdit?khid='+this.data.khid+'&clientName='+this.data.info.companyName,
      })
    }else if(type=='跟进'){
      wx.navigateTo({
        url: '../followEdit/followEdit?khid='+this.data.khid+'&clientName='+this.data.info.companyName,
      })
    }else if(type=='拜访'){
      wx.navigateTo({
        url: '../visitPlanEdit/visitPlanEdit?khid='+this.data.khid+'&clientName='+this.data.info.companyName,
      })
    }
  },
  
  /**请求数据 */
  requestData(id,page=1){
    let that = this;
    switch(this.data.active){
      case 0:
        api.curlPost('?cla=clientAppClass&fun=detail',{khid:id}).then((res)=>{
          that.setData({
            khid:res.data.info.khid,
            info:res.data.info,
            manage:res.data.manage,
            invoice:res.data.invoice,
            tags:res.data.manage.tags.join(',')
          })
        })
      break;
      case 1:
        api.curlPost('?cla=clientAppClass&fun=tabs',{khid:id,act:'staff'}).then((res)=>{
          that.setData({
            contacts:res.data
          })
        })
      break;
      case 2:
        api.curlPost('?cla=clientAppClass&fun=tabs',{khid:id,act:'file'}).then((res)=>{
          that.setData({
            attachFile:res.data,
          })
        })
      break;
      case 3:
        api.curlPost('?cla=clientAppClass&fun=tabs',{khid:id,act:'follow'}).then((res)=>{
          if(!api.isBlank(res.data)){
            that.setData({
              follow_list:res.data,
            })
          }
        })
      break;
      case 4:
        api.curlPost('?cla=clientAppClass&fun=tabs',{khid:id,act:'visitPlan'}).then((res)=>{
          if(!api.isBlank(res.data)){
            that.setData({
              visit_list:res.data,
            })
          }
        })
      break;
    }
    
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    if(api.isBlank(id)){
      wx.showToast({
        title: '未找到ID',
        icon: 'none',
      })
      setTimeout(function(){
        wx.navigateBack({
          delta: 1,
        })
      },3000)
   } else{
     this.setData({
       khid:options.id
     });
   }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestData(this.data.khid);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})